unit check_1form;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, BCButton, BCLabel, Forms, Controls, Graphics,
  Dialogs, ExtCtrls, StdCtrls, ComCtrls, fpjson, jsonparser, intf, nkTitleBar;

type

  { TfrmCheck_1 }

  TfrmCheck_1 = class(TForm)
    BCButton1: TBCButton;
    BCLabel1: TBCLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    ls: TListView;
    Memo1: TMemo;
    nkTitleBar1: TnkTitleBar;
    Panel5: TPanel;
    Panel6: TPanel;
    Panel7: TPanel;
    Panel8: TPanel;
    procedure BCButton1Click(Sender: TObject);
    procedure BCLabel1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    FKind: TnkImportKind;

  public
     procedure Log(AText:string);
     procedure do_Check_1(Sender:TObject);//进行单位登记风险分析
     procedure do_Check_11(Sender:TObject);//进行注销登记风险分析
     property Kind:TnkImportKind read FKind write FKind;//分析类型
  end;

var
  frmCheck_1: TfrmCheck_1;

implementation

uses datamodule;

{$R *.frm}

{ TfrmCheck_1 }

procedure TfrmCheck_1.FormShow(Sender: TObject);
var
  mReports:string;
begin
  if FKind = TikDanwei then
  begin
    Label1.Caption:='单位基本医疗保险参保登记风险分析';
    BCButton1.OnClick:=@do_check_1;
    //填充待分析月份列表
    //获取以分析月份列表，组合成字符串
    with dm.pubqryWork do
    begin
      Sql.Clear;
      Sql.Add('select distinct cyear,cmonth from fxmaster where shortkind="单位基本医疗参保登记风险"');
      Open;
      First;
      mReports:='';
      while not Eof do
      begin
        mReports:=mReports+inttostr(FieldByName('cyear').AsInteger)+inttostr(FieldByName('cmonth').AsInteger)+';';
        Next;
      end;
    end;
    //获取划拨数据的月份列表
    ls.Items.BeginUpdate;
    ls.Items.Clear;
    with dm.pubqryWork do
    begin
      Sql.Clear;
      Sql.Add('select distinct cyear, cmonth from danwei order by cyear desc, cmonth desc');
      Open;
      First;
      while not Eof do
      begin
        with ls.Items.Add do
        begin
          Caption:=inttostr(FieldByName('cyear').AsInteger)+'年'+inttostr(FieldByName('cmonth').AsInteger)+'月';
          SubItems.Add(inttostr(FieldByName('cyear').AsInteger));
          SubItems.Add(inttostr(FieldByName('cmonth').AsInteger));
          if Pos(SubItems.Strings[0]+SubItems.Strings[1], mReports) > 0 then
          begin
            SubItems.Add('1');
            ImageIndex:=1;
            Checked:=False;
          end
          else
          begin
            SubItems.Add('0');
            ImageIndex:=0;
            Checked:=True;
          end;
        end;
        Next;
      end;
    end;
    ls.Items.EndUpdate;
  end
  else
  if FKind = TikZhuXiao then
  begin
    Label1.Caption:='单位变更（注销）登记业务风险分析';
    BCButton1.OnClick:=@do_check_11;
    //填充待分析月份列表
    //获取以分析月份列表，组合成字符串
    with dm.pubqryWork do
    begin
      Sql.Clear;
      Sql.Add('select distinct cyear,cmonth from fxmaster where shortkind="单位变更（注销）登记业务风险"');
      Open;
      First;
      mReports:='';
      while not Eof do
      begin
        mReports:=mReports+inttostr(FieldByName('cyear').AsInteger)+inttostr(FieldByName('cmonth').AsInteger)+';';
        Next;
      end;
    end;
    //获取划拨数据的月份列表
    ls.Items.BeginUpdate;
    ls.Items.Clear;
    with dm.pubqryWork do
    begin
      Sql.Clear;
      Sql.Add('select distinct cyear, cmonth from zhuxiao order by cyear desc, cmonth desc');
      Open;
      First;
      while not Eof do
      begin
        with ls.Items.Add do
        begin
          Caption:=inttostr(FieldByName('cyear').AsInteger)+'年'+inttostr(FieldByName('cmonth').AsInteger)+'月';
          SubItems.Add(inttostr(FieldByName('cyear').AsInteger));
          SubItems.Add(inttostr(FieldByName('cmonth').AsInteger));
          if Pos(SubItems.Strings[0]+SubItems.Strings[1], mReports) > 0 then
          begin
            SubItems.Add('1');
            ImageIndex:=1;
            Checked:=False;
          end
          else
          begin
            SubItems.Add('0');
            ImageIndex:=0;
            Checked:=True;
          end;
        end;
        Next;
      end;
    end;
    ls.Items.EndUpdate;
  end;
end;

procedure TfrmCheck_1.BCLabel1Click(Sender: TObject);
begin
  Close;
end;

procedure TfrmCheck_1.BCButton1Click(Sender: TObject);
begin

end;

procedure TfrmCheck_1.Log(AText: string);
begin
  Memo1.Lines.Add(AText);
  Memo1.Update;
  Application.ProcessMessages;
end;

procedure TfrmCheck_1.do_Check_1(Sender: TObject);
var
  mCount,mTotal:integer;
  mR:TnkReport;
  mJson:TJsonObject;
  i:integer;
  mDelete:boolean;
  mChecked:boolean;
  mYear,mMonth:integer;
begin
  //先检查有没有Checked项目
  mchecked:=False;
  for i:=0 to ls.Items.Count-1 do
  begin
    if ls.Items.Item[i].Checked then
    begin
      mchecked:=True;
      break;
    end;
  end;
  if mchecked=False then
  begin
    MessageDlg('请选择要分析的月份！',mtWarning,[mbOK],0);
    Exit;
  end;

  BCButton1.Enabled:=False;
  Memo1.Lines.Clear;
  Log('开始分析……');
  for i:=0 to ls.Items.Count-1 do
  begin
    if ls.Items.Item[i].Checked then
    begin
      Log('分析'+ls.Items.Item[i].Caption+'数据……');
      mYear:=strtoint(ls.Items.Item[i].SubItems.Strings[0]);
      mMonth:=strtoint(ls.Items.Item[i].SubItems.Strings[1]);
      mDelete:=Trim(ls.Items.Item[i].SubItems.Strings[2])='1';
      mR:=TnkReport.Create(Self);
      mR.CYear:=mYear;
      mR.CMonth:=mMonth;
      mR.TipsCount:=0;
      mR.WarningCount:=0;
      mR.Title:='单位基本医疗保险登记风险分析';

      with dm.pubqryWork do
      begin
        Close;
        Sql.Clear;
        Sql.Add('select * from danwei where cyear=:cyear and cmonth=:cmonth');
        Params.ParamByName('cyear').AsInteger:=mYear;
        Params.ParamByName('cmonth').AsInteger:=mMonth;
        Open;
        mTotal:=RecordCount;
        First;

        while not Eof do
        begin
          //检查统一信用号码c3和组织机构代码c4同时为空的记录
          if (Trim(FieldByName('c3').AsString)='') and (Trim(FieldByName('c4').AsString)='') then
          begin
            mR.WarningCount:=mR.WarningCount+1;
            Log('发现风险点，单位编号'+FieldByName('c0').AsString+'：社会统一信用代码和组织机构代码同时缺失');
            mR.AddWarning(FieldByName('c0').AsString,'0');//'社会统一信用代码和组织机构代码同时为空');
            mJson:=TJsonObject.Create;
            mJson.Add(FieldByName('c0').AsString,0);
            mR.AddDetail(FieldByName('c0').AsString,StringReplace(mJson.AsJSON,'\','',[rfReplaceAll]));
            mJson.Free;
          end
          else
          //检查统一信用号码c3不为空但长度不合规的记录
          if (Trim(FieldByName('c3').AsString)<>'') and (Length(Trim(FieldByName('c0').AsString))>18) then
          begin
            mR.WarningCount:=mR.WarningCount+1;
            Log('发现风险点，单位编号'+FieldByName('c0').AsString+'：社会统一信用代码长度不足18位');
            mR.AddWarning(FieldByName('c0').AsString,'1');//'社会统一信用代码长度不足18位');
            mJson:=TJsonObject.Create;
            mJson.Add(FieldByName('c0').AsString,1);
            mR.AddDetail(FieldByName('c0').AsString,StringReplace(mJson.AsJSON,'\','',[rfReplaceAll]));
            mJson.Free;
          end;
          //检查经办人员信息为空的记录（表中没有经办人员名单！！！）
          Next;
        end;
        //检查完毕
        Log('本月风险点数量：'+inttostr(mR.WarningCount)+'  提示点数量：'+inttostr(mR.TipsCount));
        //保存分析结果
        dm.SaveFx(mR,mDelete,'单位基本医疗参保登记风险');
        dm.SaveAnalLog(Label1.Caption, 0, 0, mR.WarningCount, mR.TipsCount);
        dm.UpdateAnalCount;
        mR.Free;
      end;
    end;
  end;

  Log('----分析完毕----');
  BCButton1.Enabled:=True;
end;

procedure TfrmCheck_1.do_Check_11(Sender: TObject);
var
  mCount,mTotal:integer;
  mR:TnkReport;
  mJson:TJsonObject;
  i:integer;
  mDelete:boolean;
  mChecked:boolean;
  mYear,mMonth:integer;
begin
  //先检查有没有Checked项目
  mchecked:=False;
  for i:=0 to ls.Items.Count-1 do
  begin
    if ls.Items.Item[i].Checked then
    begin
      mchecked:=True;
      break;
    end;
  end;
  if mchecked=False then
  begin
    MessageDlg('请选择要分析的月份！',mtWarning,[mbOK],0);
    Exit;
  end;

  BCButton1.Enabled:=False;
  Memo1.Lines.Clear;
  Log('开始分析……');

  for i:=0 to ls.Items.Count-1 do
  begin
    if ls.Items.Item[i].Checked then
    begin
      Log('分析'+ls.Items.Item[i].Caption+'数据……');
      mYear:=strtoint(ls.Items.Item[i].SubItems.Strings[0]);
      mMonth:=strtoint(ls.Items.Item[i].SubItems.Strings[1]);
      mDelete:=Trim(ls.Items.Item[i].SubItems.Strings[2])='1';
      mR:=TnkReport.Create(Self);
      mR.CYear:=mYear;
      mR.CMonth:=mMonth;
      mR.TipsCount:=0;
      mR.WarningCount:=0;
      mR.Title:='单位变更（注销）登记业务风险分析';

      with dm.pubqryWork do
      begin
        Close;
        Sql.Clear;
        Sql.Add('select * from zhuxiao where cyear=:cyear and cmonth=:cmonth');
        Params.ParamByName('cyear').AsInteger:=mYear;
        Params.ParamByName('cmonth').AsInteger:=mMonth;
        Open;
        mTotal:=RecordCount;
        First;

        while not Eof do
        begin
          //检查统一信用号码c6为空的记录
          if (Trim(FieldByName('c6').AsString)='') then
          begin
            mR.WarningCount:=mR.WarningCount+1;
            Log('发现风险点，业务编码'+FieldByName('c0').AsString+'：社会统一信用代码和组织机构代码同时缺失');
            mR.AddWarning(FieldByName('c0').AsString,'0');//'社会统一信用代码和组织机构代码同时为空');
            mJson:=TJsonObject.Create;
            mJson.Add(FieldByName('c0').AsString,0);
            mR.AddDetail(FieldByName('c0').AsString,StringReplace(mJson.AsJSON,'\','',[rfReplaceAll]));
            mJson.Free;
          end
          else
          //检查统一信用号码c6不为空但长度不合规的记录
          if (Trim(FieldByName('c6').AsString)<>'') and (Length(Trim(FieldByName('c6').AsString))>18) then
          begin
            mR.WarningCount:=mR.WarningCount+1;
            Log('发现风险点，业务编码'+FieldByName('c0').AsString+'：社会统一信用代码长度不足18位');
            mR.AddWarning(FieldByName('c0').AsString,'1');//'社会统一信用代码长度不足18位');
            mJson:=TJsonObject.Create;
            mJson.Add(FieldByName('c0').AsString,1);
            mR.AddDetail(FieldByName('c0').AsString,StringReplace(mJson.AsJSON,'\','',[rfReplaceAll]));
            mJson.Free;
          end;
          //检查经办人员信息为空的记录（表中没有经办人员名单！！！）
          Next;
        end;
        //检查完毕
        Log('本月风险点数量：'+inttostr(mR.WarningCount)+'  提示点数量：'+inttostr(mR.TipsCount));
        //保存分析结果
        dm.SaveFx(mR,mDelete,'单位变更（注销）登记业务风险');
        dm.SaveAnalLog(Label1.Caption, 0, 0, mR.WarningCount, mR.TipsCount);
        dm.UpdateAnalCount;
        mR.Free;
      end;
    end;
  end;

  Log('----分析完毕----');
  BCButton1.Enabled:=True;
end;

end.

